import os
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random
import math

def generate_logo():
    """生成随机Logo并保存"""
    # 确保media和media/logo目录存在
    if not os.path.exists('media'):
        os.makedirs('media')
    if not os.path.exists('media/logo'):
        os.makedirs('media/logo')
    
    # 定义Logo尺寸
    size = (500, 500)
    # 创建透明背景的图像
    logo = Image.new('RGBA', size, color=(0, 0, 0, 0))
    draw = ImageDraw.Draw(logo)
    
    # 定义颜色
    colors = [
        (83, 109, 254),  # 主色调 #536DFE
        (61, 90, 254),   # 深色调 #3D5AFE
        (140, 158, 255)  # 浅色调 #8C9EFF
    ]
    
    # 画一个渐变的圆形背景
    for i in range(50):
        radius = 250 - i * 3
        # 在主色调和深色调之间渐变
        color_weight = i / 50
        bg_color = (
            int(colors[0][0] * (1 - color_weight) + colors[1][0] * color_weight),
            int(colors[0][1] * (1 - color_weight) + colors[1][1] * color_weight),
            int(colors[0][2] * (1 - color_weight) + colors[1][2] * color_weight),
            255
        )
        draw.ellipse(
            [(size[0]//2 - radius, size[1]//2 - radius), 
             (size[0]//2 + radius, size[1]//2 + radius)], 
            fill=bg_color
        )
    
    # 添加文字 "CR"
    try:
        # 尝试使用系统字体
        font_path = "arial.ttf"  # Windows系统默认字体
        font = ImageFont.truetype(font_path, 200)
    except IOError:
        # 如果找不到系统字体，使用默认字体
        font = ImageFont.load_default()
    
    # 绘制文字，使用白色
    text = "CR"
    text_bbox = draw.textbbox((0, 0), text, font=font)
    text_width = text_bbox[2] - text_bbox[0]
    text_height = text_bbox[3] - text_bbox[1]
    
    text_position = ((size[0] - text_width) // 2, (size[1] - text_height) // 2 - 20)
    draw.text(text_position, text, font=font, fill=(255, 255, 255, 255))
    
    # 添加小版本标记
    try:
        small_font = ImageFont.truetype(font_path, 40)
    except IOError:
        small_font = ImageFont.load_default()
    
    version_text = "v1.0"
    version_bbox = draw.textbbox((0, 0), version_text, font=small_font)
    version_width = version_bbox[2] - version_bbox[0]
    version_position = ((size[0] - version_width) // 2, text_position[1] + text_height + 10)
    draw.text(version_position, version_text, font=small_font, fill=(255, 255, 255, 200))
    
    # 添加一点随机元素
    for _ in range(10):
        angle = random.randint(0, 360)
        distance = random.randint(180, 220)
        x = size[0]//2 + int(distance * math.cos(math.radians(angle)))
        y = size[1]//2 + int(distance * math.sin(math.radians(angle)))
        dot_radius = random.randint(5, 15)
        dot_color = random.choice(colors)
        draw.ellipse(
            [(x - dot_radius, y - dot_radius), (x + dot_radius, y + dot_radius)], 
            fill=dot_color + (200,)
        )
    
    # 添加一点模糊效果
    logo = logo.filter(ImageFilter.GaussianBlur(radius=1))
    
    # 保存为PNG文件（保留透明度）
    logo_path = 'media/logo/cursor_rules_logo.png'
    logo.save(logo_path)
    print(f"Logo已生成并保存到：{logo_path}")
    return logo_path

if __name__ == "__main__":
    try:
        logo_path = generate_logo()
        print(f"成功创建Logo：{logo_path}")
        # 显示Logo（如果在支持GUI的环境中）
        Image.open(logo_path).show()
    except Exception as e:
        print(f"创建Logo时出错：{e}") 